package com.maiji.cloud.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.maiji.cloud.entities.login.FoodMaterialEntity;
import com.maiji.cloud.entities.login.FoodMaterialTypeLable;
import com.maiji.cloud.mapper.FoodMaterialTypeLableMapper;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.Status;
import com.maiji.cloud.response.login.FoodTypeResData;
import com.maiji.cloud.service.FoodMaterialService;
import com.maiji.cloud.service.FoodMaterialTypeLableService;
import com.maiji.cloud.utils.UUID_MD5;
import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class FoodMaterialTypeLableServiceImpl extends ServiceImpl<FoodMaterialTypeLableMapper,
        FoodMaterialTypeLable> implements FoodMaterialTypeLableService {

    @Autowired
    private FoodMaterialTypeLableMapper foodMaterialTypeLableMapper;
    @Autowired
    private FoodMaterialService foodMaterialService;

    @Override
    public BaseDataResDto<List<FoodTypeResData>> findAllFoodTypes(Boolean isFindFoods) {
        List<FoodTypeResData> allFoodTypes = foodMaterialTypeLableMapper.findAllFoodTypes();
        if (isFindFoods) {
            Map<String, List<FoodMaterialEntity>> foodMap = foodMaterialService.selectList(new EntityWrapper<>())
                    .stream().collect(Collectors.groupingBy(FoodMaterialEntity::getTypeLabelId));
            allFoodTypes = allFoodTypes.stream().map(ftrs ->
                    ftrs.setChildren(ftrs.getChildren().stream().map(cftrs -> {
                        List<FoodMaterialEntity> foods = foodMap.get(cftrs.getValue());
                        if (foods == null || foods.size() == 0) return cftrs;
                        else return cftrs.setChildren(foods.stream().map(fme ->
                                new FoodTypeResData().setValue(fme.getUuId()).setLabel(fme.getName())).collect(Collectors.toList()));
                    }).collect(Collectors.toList()))
            ).collect(Collectors.toList());
        }
        return new BaseDataResDto<List<FoodTypeResData>>(Status.SUCCESS).setData(allFoodTypes);
    }

    @Override
    public BaseResDto editFoodType(FoodTypeResData foodType) {
        if (StringUtil.isBlank(foodType.getValue())) {
            FoodMaterialTypeLable firstFoodType = new FoodMaterialTypeLable()
                    .setUuId(UUID_MD5.getUUID()).setName(foodType.getLabel());
            if (! insert(firstFoodType)) return new BaseResDto(Status.ERROR.setMessage("保存一级分类失败"));
        } else {
            FoodMaterialTypeLable firstFoodType = new FoodMaterialTypeLable()
                    .setUuId(foodType.getValue()).setName(foodType.getLabel());
            if (! updateById(firstFoodType)) return new BaseResDto(Status.ERROR.setMessage("编辑一级分类失败"));
        }
        List<FoodTypeResData> childrenList = foodType.getChildren();
        if (childrenList != null && childrenList.size() == 1) {
            FoodTypeResData children = childrenList.get(0);
            if (StringUtil.isBlank(children.getValue())) {
                FoodMaterialTypeLable secondFoodType = new FoodMaterialTypeLable()
                        .setUuId(UUID_MD5.getUUID()).setName(children.getLabel()).setParentId(foodType.getValue());
                if (! insert(secondFoodType)) return new BaseResDto(Status.ERROR.setMessage("保存二级分类失败"));
            } else {
                FoodMaterialTypeLable secondFoodType = new FoodMaterialTypeLable()
                        .setUuId(children.getValue()).setName(children.getLabel()).setParentId(foodType.getValue());
                if (! updateById(secondFoodType)) return new BaseResDto(Status.ERROR.setMessage("编辑二级分类失败"));
            }
        }
        return new BaseResDto(Status.SUCCESS);
    }

}
